//
// 通知定时器使用示例
// Created by kenny on 25-11-6.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>  // 用于 sleep 函数
#include "../platform/comm_timer.h"
#include "../mcp_logger.h"

// 自定义设备信息数据结构
typedef struct {
    char device_id[64];
    char device_name[64];
    float temperature;
    float humidity;
    int battery_level;
    unsigned long report_count;
} my_device_info_t;

// 模拟获取实时数据
void update_device_data(my_device_info_t* info) {
    // 模拟数据变化
    info->temperature = 20.0f + (rand() % 200) / 10.0f;  // 20.0-40.0°C
    info->humidity = 30.0f + (rand() % 500) / 10.0f;     // 30.0-80.0%
    info->battery_level = 50 + (rand() % 50);           // 50-100%
    info->report_count++;
}

// 自定义上报函数实现
void my_device_info_func(void* device_info_data) {
    my_device_info_t* info = (my_device_info_t*)device_info_data;

    // 更新设备数据
    update_device_data(info);

    // 模拟上报逻辑
    LOG_INFO("[设备信息上报] ID: %s, 名称: %s\n", info->device_id, info->device_name);
    LOG_INFO("  温度: %.1f°C, 湿度: %.1f%%, 电量: %d%%, 上报次数: %lu\n",
           info->temperature, info->humidity, info->battery_level, info->report_count);
    LOG_INFO("  时间戳: %ld\n\n", (long)time(NULL));

    // 这里可以添加实际的上报逻辑，如：
    // - 发送到MQTT服务器
    // - 写入本地文件
    // - 通过HTTP POST到云平台
    // - 通过串口发送到其他设备
}

// 另一个示例：精简版上报函数
void simple_device_info_func(void* device_info_data) {
    my_device_info_t* info = (my_device_info_t*)device_info_data;
    update_device_data(info);

    LOG_INFO("[精简上报] %s: %.1f°C, %d%%电量 (第%lu次)\n",
           info->device_name, info->temperature, info->battery_level, info->report_count);
}

// 测试动态启停定时器
void test_dynamic_control(void) {
    LOG_INFO("=== 测试动态启停功能 ===\n");

    my_device_info_t device_data = {
        .device_id = "DEVICE_DYNAMIC_001",
        .device_name = "动态测试设备",
        .temperature = 25.0f,
        .humidity = 60.0f,
        .battery_level = 80,
        .report_count = 0
    };

    timer_config_t config = {
        .enabled = true,
        .interval_sec = 2,  // 2秒间隔
        .user_func = simple_device_info_func,
        .user_data = &device_data
    };

    comm_timer_t* timer = comm_timer_create(&config);
    if (!timer) {
        LOG_INFO("创建定时器失败\n");
        return;
    }

    // 测试多次启停
    for (int i = 0; i < 3; i++) {
        LOG_INFO("第%d次启动定时器...\n", i + 1);
        if (comm_timer_start(timer)) {
            sleep(6);  // 运行6秒（约3次上报）
            LOG_INFO("停止定时器...\n");
            comm_timer_stop(timer);
            sleep(2);  // 停止2秒
        }
    }

    comm_timer_destroy(timer);
    LOG_INFO("动态测试完成\n\n");
}

// 测试多个定时器同时运行
void test_multiple_timers(void) {
    LOG_INFO("=== 测试多个定时器 ===\n");

    my_device_info_t device1_data = {
        .device_id = "MULTI_DEVICE_001",
        .device_name = "快速设备",
        .report_count = 0
    };

    my_device_info_t device2_data = {
        .device_id = "MULTI_DEVICE_002",
        .device_name = "慢速设备",
        .report_count = 0
    };

    // 快速定时器：每1秒上报
    timer_config_t config1 = {
        .enabled = true,
        .interval_sec = 1,
        .user_func = simple_device_info_func,
        .user_data = &device1_data
    };

    // 慢速定时器：每3秒上报
    timer_config_t config2 = {
        .enabled = true,
        .interval_sec = 3,
        .user_func = my_device_info_func,
        .user_data = &device2_data
    };

    comm_timer_t* timer1 = comm_timer_create(&config1);
    comm_timer_t* timer2 = comm_timer_create(&config2);

    if (!timer1 || !timer2) {
        LOG_INFO("创建定时器失败\n");
        return;
    }

    LOG_INFO("启动两个定时器：快速(1秒) + 慢速(3秒)\n");
    comm_timer_start(timer1);
    comm_timer_start(timer2);

    // 运行15秒
    sleep(15);

    LOG_INFO("停止所有定时器...\n");
    comm_timer_stop(timer1);
    comm_timer_stop(timer2);

    comm_timer_destroy(timer1);
    comm_timer_destroy(timer2);
    LOG_INFO("多定时器测试完成\n\n");
}
// int test_main() {
int test_timer() {
    LOG_INFO("=== 设备信息上报定时器演示 ===\n\n");

    // 设置随机种子
    srand((unsigned int)time(NULL));

    // 示例1：基本使用
    LOG_INFO("=== 示例1：基本定时上报 ===\n");

    my_device_info_t device_data = {
        .device_id = "DEVICE_001",
        .device_name = "智能温湿度传感器",
        .temperature = 25.5f,
        .humidity = 65.0f,
        .battery_level = 95,
        .report_count = 0
    };

    timer_config_t config = {
        .enabled = true,
        .interval_sec = 3,  // 3秒间隔
        .user_func = my_device_info_func,
        .user_data = &device_data
    };

    // 创建定时器
    comm_timer_t* timer = comm_timer_create(&config);
    if (!timer) {
        LOG_INFO("错误：创建定时器失败\n");
        return -1;
    }
    LOG_INFO("定时器创建成功\n");

    // 启动定时器
    if (comm_timer_start(timer)) {
        LOG_INFO("定时器启动成功，每%d秒上报一次设备信息\n", config.interval_sec);
        LOG_INFO("程序运行中，10秒后自动停止...\n\n");

        // 主线程等待10秒（模拟程序运行）
        sleep(10);

        // 停止定时器
        comm_timer_stop(timer);
        LOG_INFO("定时器已停止\n");
    } else {
        LOG_INFO("错误：定时器启动失败\n");
    }

    // 销毁定时器
    comm_timer_destroy(timer);
    LOG_INFO("定时器已销毁\n\n");

    // 示例2：测试动态启停
    test_dynamic_control();

    // 示例3：测试多个定时器
    test_multiple_timers();

    LOG_INFO("=== 所有测试完成 ===\n");
    return 0;
}